﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Google.GData.Spreadsheets;
using Google.GData.Client;

namespace Copycat
{
    public partial class DiscountManagment : System.Web.UI.Page
    {
        public struct faceRequester
        {
            public string name;
            public string tel;
            public string email;
            public string facebookName;
            public string status;
        }
        private List<faceRequester> faceRequesters;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (IsPostBack)
            {

                faceRequesters = (List<faceRequester>)Session["requesters"];
                return;
            }

            AtomEntryCollection rows = DatabaseManager.RetrieveRows("discountreq");

            faceRequester s = new faceRequester();

            faceRequesters = new List<faceRequester>();
            foreach (ListEntry ae in rows)
            {

                ListBox1.Items.Add(ae.Elements[0].Value);

                s = new faceRequester();
                s.name = ae.Elements[0].Value;
                s.email = ae.Elements[2].Value;
                s.tel = ae.Elements[1].Value;
                s.facebookName = ae.Elements[3].Value;
                faceRequesters.Add(s);
            }
            Session["requesters"] = faceRequesters;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int i = ListBox1.SelectedIndex;
            if (i == -1)
                return;
            faceRequester req = faceRequesters[i];



            if (DatabaseManager.AddDiscountID(req.name, req.tel, req.facebookName, req.email, txtID.Text))
            {
                DatabaseManager.DeleteRecord("discountreq", "tel", req.tel);
                ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
                txtEmail.Text = "";
                txtFacebook.Text = "";
                txtID.Text = "";
                txtTel.Text = "";
            }
        }

        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = ListBox1.SelectedIndex;
            if (i == -1)
                return;
            faceRequester req = faceRequesters[i];

            txtTel.Text = req.tel;
            txtFacebook.Text = req.facebookName;
            txtEmail.Text = req.email;
        }
    }
}
